package com.longteng.auth.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.longteng.auth.service.SysRoleService;
import com.longteng.common.config.exception.SysRoleException;
import com.longteng.common.result.Result;
import com.longteng.model.system.SysRole;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author shkstart
 * @Description
 * @create 2024--01--06--17:14
 */
@Api("角色管理接口")
@RestController
@RequestMapping("/admin/system/sysRole")
public class SysRoleController {
    @Autowired
    SysRoleService sysRoleService;

    //查询所有角色
    @ApiOperation("查询所有角色")
    @GetMapping("/findAll")
    public Result findAll(){
        List<SysRole> list = sysRoleService.list();
        try {
            int i = 10 /0 ;
        } catch (Exception e) {
            throw new SysRoleException(303,"恭喜你出错了");
        }
        return Result.success(list);
    }

    //分页查询
    @ApiOperation("分页查询角色")
    @GetMapping("/page")
    public Result page(int page,int pageSize,String roleName){
        //构造分页构造器
        Page pageInfo = new Page(page,pageSize);
        //构造分页查询条件
        LambdaQueryWrapper<SysRole> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //根据name值进行模糊查询
        lambdaQueryWrapper.like(StringUtils.hasText(roleName),SysRole::getRoleName,roleName);
        //按照更新时间对用户进行排序
        lambdaQueryWrapper.orderByDesc(SysRole::getUpdateTime);
        //执行排序
        sysRoleService.page(pageInfo,lambdaQueryWrapper);

        return Result.success(pageInfo);
    }

    //添加角色
    @ApiOperation("添加角色")
    @PostMapping("/addRole")
    public Result addRole(@RequestBody SysRole sysRole){
        boolean save = sysRoleService.save(sysRole);
        if(save){
            return Result.success();
        }else {
            return Result.fail();
        }

    }

   //根据id查询角色
    @ApiOperation("根据id查询角色")
    @GetMapping("/getRole")
    public Result getRole(Long id){
        SysRole sysRole = sysRoleService.getById(id);
        return Result.success(sysRole);
    }

    //修改角色
    @ApiOperation("根据id修改角色")
    @PutMapping("/updateRole")
    public Result updateRole(@RequestBody SysRole sysRole){
        boolean update = sysRoleService.updateById(sysRole);
        if(update){
            return Result.success();
        }else {
            return Result.fail();
        }
    }

    //根据id删除
    @ApiOperation("根据id删除角色")
    @DeleteMapping("/delete")
    public Result deleteById(Long id){
        boolean remove = sysRoleService.removeById(id);
        if(remove){
            return Result.success();
        }else {
            return Result.fail();
        }
    }

    //根据id批量删除
    @ApiOperation("根据id批量删除")
    @DeleteMapping("/batchRemove")
    public Result batchRemove(@RequestParam List<Long> ids){
        boolean removeByIds = sysRoleService.removeByIds(ids);
        if(removeByIds){
            return Result.success();
        }else {
            return Result.fail();
        }
    }
}
